/*
 * Copyright 2020-2025 the original author or authors.
 * You cannot use this file unless authorized by the author.
 */

package org.ipig.constants;

import org.apache.commons.lang3.StringUtils;

import java.time.format.DateTimeFormatter;

/**
 * 日期和时间常量
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $Id: DateTimeCnst.java 15 2020-04-21 12:04:17Z ts <comchnts@163.com> $
 * @since 1.0
 */
public class DateTimeCnst {
    /*** 时间切片模式关键字***/
    public final static String KEY_TIME_PATTERN = "p";
    /*** 之前的时间关键字***/
    public final static String KEY_TIME_BEFORE = "b";
    /* 之后的时间关键字***/
    public final static String KEY_TIME_AFTER = "a";

    /*** 年：yyyy ***/
    public final static String YEAR = "yyyy";
    /*** 月：yyyyMM ***/
    public final static String MONTH = "yyyyMM";
    public final static String DAYS = "yyyyMMdd";
    public final static String HOURS = "yyyyMMddHH";
    public final static String MINUTES = "yyyyMMddHHmm";
    public final static String SECONDS = "yyyyMMddHHmmss";
    //日期关键字不分区大小写
    public static final String[] DATE_KEY_IGNORE_CASE = {DateTimeCnst.Pattern.YEARS.code,
            DateTimeCnst.Pattern.DAYS.code, DateTimeCnst.Pattern.HOURS.code, DateTimeCnst.Pattern.SECONDS.code};
    //日期关键字对大小写敏感
    public static final String[] DATE_KEY_CASE_SENSITIVE = {DateTimeCnst.Pattern.MONTHS.code,
            DateTimeCnst.Pattern.MINUTES.code};
    /**
     * 默认日期格式： yyyyMMdd
     */
    public final static String DATE = DAYS;
    /**
     * 默认时间格式： HHmmss
     */
    public final static String TIME = "HHmmss";
    /**
     * 默认日期时间格式： yyyyMMddHHmmss
     */
    public final static String DATE_TIME = SECONDS;
    /**
     * ISO的日历体系格式： yyyy-MM-dd
     */
    public final static String ISO_DATE = "yyyy-MM-dd";
    /**
     * ISO的日历体系格式： HH:mm:ss.SSS
     */
    public final static String ISO_TIME = "HH:mm:ss.SSS";
    /**
     * ISO的日历体系格式：yyyy-MM-dd'T'HH:mm:ss.SSS
     */
    public final static String ISO_DATE_TIME = "yyyy-MM-dd'T'HH:mm:ss.SSS";


    /**
     * 时间模式
     */
    public enum Pattern {
        YEARS("y", DateTimeCnst.Style.YEARS, DateTimeCnst.YEAR, 31536000L, DateTimeCnst.YEAR.length(), "y(年)"),
        MONTHS("M", DateTimeCnst.Style.MONTHS, DateTimeCnst.MONTH, 2678400L, DateTimeCnst.MONTH.length(), "M(月)"),
        DAYS("d", DateTimeCnst.Style.DAYS, DateTimeCnst.DAYS, 86400L, DateTimeCnst.DAYS.length(), "d(天)"),
        HOURS("h", DateTimeCnst.Style.HOURS, DateTimeCnst.HOURS, 3600L, DateTimeCnst.HOURS.length(), "h(小时)"),
        MINUTES("m", DateTimeCnst.Style.MINUTES, DateTimeCnst.MINUTES, 60L, DateTimeCnst.MINUTES.length(), "m(分钟)"),
        SECONDS("s", DateTimeCnst.Style.SECONDS, DateTimeCnst.SECONDS, 1L, DateTimeCnst.SECONDS.length(), "s(秒)");
        //代码
        public final String code;
        //Style
        public final DateTimeCnst.Style style;
        //Pattern模式，格式
        public final String pattern;
        //转化成秒
        public final Long seconds;
        public final Integer length;
        public final String title;

        Pattern(String code, DateTimeCnst.Style style, String pattern, Long seconds, Integer length, String title) {
            this.code = code;
            this.style = style;
            this.pattern = pattern;
            this.seconds = seconds;
            this.length = length;
            this.title = title;
        }

        public static Pattern parse(String code) {
            for (Pattern ptn : Pattern.values()) {
                if (StringUtils.equals(ptn.code, code)) {
                    return ptn;
                }
            }
            return null;
        }
    }

    public enum Formatter {
/*
        BASIC_ISO_DATE 20111203
        ISO_LOCAL_DATE 2011-12-03
        ISO_OFFSET_DATE 2011-12-03+01:00
        ISO_DATE '2011-12-03+01:00'; '2011-12-03'
        ISO_LOCAL_TIME 10:15:30
        ISO_OFFSET_TIME 10:15:30+01:00
        ISO_TIME '10:15:30+01:00'; '10:15:30'
        ISO_LOCAL_DATE_TIME 2011-12-03T10:15:30
        ISO_OFFSET_DATE_TIME 2011-12-03T10:15:30+01:00
        ISO_ZONED_DATE_TIME  2011-12-03T10:15:30+01:00[Europe/Paris]
        ISO_DATE_TIME 2011-12-03T10:15:30+01:00[Europe/Paris]
        ISO_ORDINAL_DATE 2012-337
        ISO_WEEK_DATE 2012-W48-6
        ISO_INSTANT 2011-12-03T10:15:30Z
        */
        /**
         * 格式：10:15:30
         */
        ISO_LOCAL_TIME(DateTimeFormatter.ISO_LOCAL_TIME),
        /**
         * 格式：2011-12-03
         */
        ISO_LOCAL_DATE(DateTimeFormatter.ISO_LOCAL_DATE),
        /**
         * 格式：20111203
         */
        BASIC_ISO_DATE(DateTimeFormatter.BASIC_ISO_DATE),
        /**
         * 格式：2011-12-03T10:15:30
         */
        ISO_LOCAL_DATE_TIME(DateTimeFormatter.ISO_LOCAL_DATE_TIME),
        /**
         * 格式：2011-12-03T10:15:30Z
         */
        ISO_INSTANT(DateTimeFormatter.ISO_INSTANT);

        /**
         * code
         */
        private final DateTimeFormatter code;


        /**
         * @param code String
         */
        Formatter(DateTimeFormatter code) {
            this.code = code;
        }

        /**
         * @return the code
         */
        public DateTimeFormatter getCode() {
            return code;
        }
    }

    /**
     * Style
     *
     * @author <a href="mailto:tansheng@zjport.gov.cn">tansheng</a>
     * @version $Id: DateTimeCnst.java 2063 2019-11-08 07:24:08Z tansheng $
     * @since 1.0
     */
    public enum Style {
        /**
         * 年
         */
        YEARS,
        /**
         * 月
         */
        MONTHS,
        /**
         * 周
         */
        WEEKS,
        /**
         * 日
         */
        DAYS,
        /**
         * 时
         */
        HOURS,
        /**
         * 分
         */
        MINUTES,
        /**
         * 秒
         */
        SECONDS;
    }
}
